-- UIDungeonFountain
-- Created by chengb Mar/11/2015
-- 幸运喷泉界面

-- UIDungeonFountain 继承自Layer
UIDungeonFountain = class("UIDungeonFountain", function()
    return cc.Layer:create();
end);

function UIDungeonFountain.create(pos)
    return UIDungeonFountain.new(pos);
end

-- 内部函数声明
local resize;

-- 构造函数
function UIDungeonFountain:ctor(pos)
    self:setName("UIDungeonFountain");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/fountain/DungeonFountain.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 记录格子所在位置
    self.pos = pos;

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIDungeonFountain:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIDungeonFountain:registerEventCallback()
    -- 关注领取喷泉奖励的事件
    EventMgr.register("UIDungeonFountain", event.TAKE_FOUNTAIN_BONUS, function(args)
        self:whenTakeBonus(args);
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIDungeonFountain 界面析构清理");

            EventMgr.removeAll("UIDungeonFountain");

            if not UIDungeonMgr:getCurLevel() then
                return;
            end
            UIDungeonMgr:getCurLevel():showMenu();
        elseif eventType == "enter" then
            if not UIDungeonMgr:getCurLevel() then
                return;
            end
            UIDungeonMgr:getCurLevel():hideMenu();
        end
    end);
end

-- 注册点击事件
function UIDungeonFountain:registerTouchEvent()
    local node = self.node;

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIDungeonFountain");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            -- 判断能否领取奖励
            local ret = DungeonFountainM.canTakeBonus(self.pos);
            if ret ~= true then
                if ret == "sp_lack" then
                    -- 探索点不足
                    local fieldName = FieldsM.getFieldName("search_point");
                    local msg = string.format(getLocStr("something_not_enough"), fieldName);
                    alert(msg);
                else
                    trace("UIDungeonFountain", "无法领取喷泉奖励，原因：" .. tostring(ret));
                end
                return;
            end

            -- 领取喷泉奖励
            DungeonActionM.go("take_fountain_bonus", self.pos);
        end
    end
    btnOk:addTouchEventListener(onOkClick);
end

-- 重绘
function UIDungeonFountain:redraw()
    local CT = findChildByName(self.node, "CT");
    local BT = findChildByName(self.node, "BT");

    -- 显示标题
    local kerning = 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning = -3;
    end
    local titleLabel = findChildByName(CT, "title_node/title");
    titleLabel:setString(getLocStr("fountain_text"));
    TextStyleM.setArtWordTitleStyle(titleLabel);

    -- 显示建筑图标
    local fountainImg = findChildByName(CT, "fountain_icon");
    fountainImg:loadTexture(getLevelItemIconPath("fountain"));

    -- 显示功能描述
    local tipLabel = findChildByName(CT, "tip");
    TextStyleM.setTextStyle(tipLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning);
    applyString(tipLabel, getLocStr("fountain_tip"));

    -- 显示拥有的探索点
    local amount = ME.user:queryAttrib("search_point");

    -- 显示探索点消耗
    local spCost = DungeonFountainM.calcSPCost();
    local costLabel = findChildByName(CT, "cost");
    TextStyleM.setTextStyle(costLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);

    -- 探索点足够蓝色显示，否则红色显示
    if amount >= spCost then
        applyString(costLabel, string.format(getLocStr("fountain_cost_desc1"), spCost));
    else
        applyString(costLabel, string.format(getLocStr("fountain_cost_desc2"), spCost));
    end

    -- 显示确定按钮
    local btnOk = findChildByName(CT, "btn_ok");
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("btn_text_ok"));

    -- 显示返回按钮
    local btnBack = findChildByName(BT, "btn_back");
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end

-- 领取喷泉奖励的回调
function UIDungeonFountain:whenTakeBonus(args)
    -- 关闭当前界面
    UIMgr.getCurrentScene():removeFormByName("UIDungeonFountain");

    local bonus = args["bonus"];

    -- TODO 需要播放动画
    if bonus[1] == 1 then
        -- 物品奖励
        local name = ItemM.query(bonus[2], "name");
        local msg = string.format(getLocStr("item_bonus_format"), name, bonus[3]);
        alert(msg);
    elseif bonus[1] == 2 then
        -- 属性奖励
        local name = FieldsM.getFieldName(bonus[2]);
        local msg = string.format(getLocStr("attrib_bonus_format"), name, bonus[3]);
        alert(msg);
    else
        -- 其他情况暂不处理
    end
end
